<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: User
  Date: 2024/5/15
  Time: 18:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta http-equiv="X-UA-Compatible" content="ie=edge"/>
    <title>backend</title>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/bootstarp/css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/index.css"/>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/nmms.css"/>
    <script src="${pageContext.request.contextPath}/js/jquery.js" type="text/javascript" charset="utf-8"></script>
    <script src="${pageContext.request.contextPath}/bootstarp/js/bootstrap.js" type="text/javascript"
            charset="utf-8"></script>
    <script src="${pageContext.request.contextPath}/js/userSetting.js" type="text/javascript" charset="utf-8"></script>
    <script src="${pageContext.request.contextPath}/js/bootstrap-paginator.js" type="text/javascript"
            charset="utf-8"></script>
    <script>
        $(function () {
            // 分页
            $("#deptPageHelper").bootstrapPaginator({
                bootstrapMajorVersion: 3,
                currentPage: "${deptPage.pageNum == 0 ? 1 : deptPage.pageNum}",
                // 总页数
                totalPages: "${deptPage.pages == 0 ? 1 : deptPage.pages}",
                size: "normal",
                alignment: "center",
                // 分页事件,当用户点击页码之后触发的事件
                pageUrl: function (type, page, current) {
                    return "${pageContext.request.contextPath}/dept/findAll?pageNo=" + page;
                }
            });

            // 添加一级部门
            $(".addDept").click(function () {
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/dept/addFatherDept",
                    data: {
                        "name": $("#dept-name").val(),
                        "remark": $("#dept-remark").val()
                    },
                    dataType: "json",
                    success: function (result) {
                        console.log(result);
                        if (result.responseCode == "2001") {
                            location.href = "${pageContext.request.contextPath}/dept/findAll?pageNo=${deptPage.pageNum == 0 ? 1 : deptPage.pageNum}";
                        } else {
                            $("#errorMsg").tooltip({
                                title: "error",
                                placement: "center",
                                template: "<div class='tooltip errorMsg'>" + result.message + "</div>"
                            }).tooltip("show");
                        }
                    }
                });
            });

            // 设置添加子部门时的默认值
            $(".doAddSonDept").click(function () {
                $("#father-id").val($(this).attr("data-id"));
                $("#father-name").val($(this).attr("data-name"));
            });

            // 添加子部门
            $(".addSonDept").click(function () {
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/dept/addSonDept",
                    data: {
                        "fatherId": $("#father-id").val(),
                        "name": $("#son-name").val(),
                        "remark": $("#son-remark").val()
                    },
                    dataType: "json",
                    success: function (result) {
                        if (result.responseCode == "2001") {
                            location.href = "${pageContext.request.contextPath}/dept/findAll?pageNo=${deptPage.pageNum == 0 ? 1 : deptPage.pageNum}";
                        } else {
                            $("#errorMsg").tooltip({
                                title: "error",
                                placement: "center",
                                template: "<div class='tooltip errorMsg'>" + result.message + "</div>"
                            }).tooltip("show");
                        }
                    }
                });
            });

            // 修改设置默认值
            $(".doModifyDept").click(function () {
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/dept/findById",
                    data: {"id": $(this).attr("data-id")},
                    dataType: "json",
                    success: function (result) {
                        if (result.responseCode == "2001") {
                            $("#modify-id").val(result.returnObject.deptId);
                            $("#modify-name").val(result.returnObject.deptName);
                            $("#modify-remark").val(result.returnObject.remark);
                        } else {
                            $("#errorMsg").tooltip({
                                title: "error",
                                placement: "center",
                                template: "<div class='tooltip errorMsg'>" + result.message + "</div>"
                            }).tooltip("show");
                        }
                    }
                });
            });

            // 修改部门
            $(".modifyDept").click(function () {
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/dept/modifyDept",
                    data: {
                        "id": $("#modify-id").val(),
                        "name": $("#modify-name").val(),
                        "remark": $("#modify-remark").val()
                    },
                    dataType: "json",
                    success: function (result) {
                        if (result.responseCode == "2001") {
                            location.href = "${pageContext.request.contextPath}/dept/findAll?pageNo=${deptPage.pageNum == 0 ? 1 : deptPage.pageNum}";
                        } else {
                            $("#errorMsg").tooltip({
                                title: "error",
                                placement: "center",
                                template: "<div class='tooltip errorMsg'>" + result.message + "</div>"
                            }).tooltip("show");
                        }
                    }
                });
            });

            // 启用部门
            $(".doEnable").click(function () {
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/dept/enableDept",
                    data: {"id": $(this).attr("data-id")},
                    dataType: "json",
                    success: function (result) {
                        if (result.responseCode == "2001") {
                            location.href = "${pageContext.request.contextPath}/dept/findAll?pageNo=${deptPage.pageNum == 0 ? 1 : deptPage.pageNum}";
                        } else {
                            $("#errorMsg").tooltip({
                                title: "error",
                                placement: "center",
                                template: "<div class='tooltip errorMsg'>" + result.message + "</div>"
                            }).tooltip("show");
                        }
                    }
                });
            });

            // 禁用部门
            $(".doDisable").click(function () {
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/dept/disableDept",
                    data: {"id": $(this).attr("data-id")},
                    dataType: "json",
                    success: function (result) {
                        if (result.responseCode == "2001") {
                            location.href = "${pageContext.request.contextPath}/dept/findAll?pageNo=${deptPage.pageNum == 0 ? 1 : deptPage.pageNum}";
                        } else {
                            $("#errorMsg").tooltip({
                                title: "error",
                                placement: "center",
                                template: "<div class='tooltip errorMsg'>" + result.message + "</div>"
                            }).tooltip("show");
                        }
                    }
                });
            });
        })
    </script>
</head>

<body>

<!-- 部门管理 -->
<div class="panel panel-default" id="departmentSet">
    <div class="panel-heading">
        <h3 class="panel-title">部门管理&nbsp;&nbsp;&nbsp;<span id="errorMsg"></span></h3>
    </div>
    <div class="panel-body">
        <input type="button" value="添加部门" class="btn btn-primary" id="doAddDept">
        <br>
        <br>
        <div class="modal fade" tabindex="-1" id="Dept">
            <!-- 窗口声明 -->
            <div class="modal-dialog modal-lg">
                <!-- 内容声明 -->
                <div class="modal-content">
                    <!-- 头部、主体、脚注 -->
                    <div class="modal-header">
                        <button class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">添加部门</h4>
                    </div>
                    <div class="modal-body text-center">
                        <div class="row text-right">
                            <label for="dept-name" class="col-sm-4 control-label">部门名称：</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="dept-name">
                            </div>
                        </div>
                        <br>
                        <div class="row text-right">
                            <label for="dept-remark" class="col-sm-4 control-label">部门职能：</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="dept-remark">
                            </div>
                        </div>
                        <br>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-primary addDept">添加</button>
                        <button class="btn btn-primary cancel" data-dismiss="modal">取消</button>
                    </div>
                </div>
            </div>
        </div>

        <!-- 添加子部门模态框 -->
        <div class="modal fade" tabindex="-1" id="sonDept">
            <!-- 窗口声明 -->
            <div class="modal-dialog modal-lg">
                <!-- 内容声明 -->
                <div class="modal-content">
                    <!-- 头部、主体、脚注 -->
                    <div class="modal-header">
                        <button class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">添加子部门</h4>
                    </div>
                    <div class="modal-body text-center">
                        <div class="row text-right">
                            <label for="father-name" class="col-sm-4 control-label">父部门名称：</label>
                            <div class="col-sm-4">
                                <input type="hidden" id="father-id">
                                <input type="text" class="form-control" id="father-name" value="市场部"
                                       readonly="readonly">
                            </div>
                        </div>
                        <br>
                        <div class="row text-right">
                            <label for="son-name" class="col-sm-4 control-label">部门名称：</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="son-name">
                            </div>
                        </div>
                        <br>
                        <div class="row text-right">
                            <label for="son-remark" class="col-sm-4 control-label">部门职能：</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="son-remark">
                            </div>
                        </div>
                        <br>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-primary addSonDept">添加</button>
                        <button class="btn btn-primary cancel" data-dismiss="modal">取消</button>
                    </div>
                </div>
            </div>
        </div>


        <!-- 修改部门模态框 -->
        <div class="modal fade" tabindex="-1" id="modifyDept">
            <!-- 窗口声明 -->
            <div class="modal-dialog modal-lg">
                <!-- 内容声明 -->
                <div class="modal-content">
                    <!-- 头部、主体、脚注 -->
                    <div class="modal-header">
                        <button class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">修改部门</h4>
                    </div>
                    <div class="modal-body text-center">
                        <div class="row text-right">
                            <label for="modify-id" class="col-sm-4 control-label">编号：</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="modify-id" value="1" readonly="readonly">
                            </div>
                        </div>
                        <br>
                        <div class="row text-right">
                            <label for="modify-name" class="col-sm-4 control-label">部门名称：</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="modify-name">
                            </div>
                        </div>
                        <br>
                        <div class="row text-right">
                            <label for="modify-remark" class="col-sm-4 control-label">部门职能：</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="modify-remark">
                            </div>
                        </div>
                        <br>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-primary modifyDept">修改</button>
                        <button class="btn btn-primary cancel" data-dismiss="modal">取消</button>
                    </div>
                </div>
            </div>
        </div>

        <div class="show-list">
            <table class="table table-bordered table-hover" style='text-align: center;'>
                <thead>
                <tr class="text-danger">
                    <th class="text-center">序号</th>
                    <th class="text-center">部门编号</th>
                    <th class="text-center">部门名称</th>
                    <th class="text-center">部门职能</th>
                    <th class="text-center">所属部门</th>
                    <th class="text-center">部门状态</th>
                    <th class="text-center">操作</th>
                </tr>
                </thead>
                <tbody id="tb">
                <c:forEach items="${deptPage.list}" var="dept">
                    <tr>
                        <td>${dept.deptId}</td>
                        <td>${dept.deptNo}</td>
                        <td>${dept.deptName}</td>
                        <td>${dept.remark}</td>
                        <td>
                            <c:if test="${dept.fatherDept == null}">--</c:if>
                            <c:if test="${dept.fatherDept != null}">${dept.fatherDept.deptName}</c:if>
                        </td>
                        <td>
                            <c:if test="${dept.isValid == 1}">启用</c:if>
                            <c:if test="${dept.isValid == 0}">禁用</c:if>
                        </td>
                        <td class="text-center">
                            <input type="button" class="btn btn-info btn-sm doAddSonDept" data-id="${dept.deptId}"
                                   data-name="${dept.deptName}" value="添加子部门">
                            <input type="button" class="btn btn-warning btn-sm doModifyDept" data-id="${dept.deptId}"
                                   value="修改">
                            <c:if test="${dept.isValid == 1}">
                                <input type="button" class="btn btn-danger btn-sm doDisable" data-id="${dept.deptId}"
                                       value="禁用">
                            </c:if>
                            <c:if test="${dept.isValid == 0}">
                                <input type="button" class="btn btn-success btn-sm doEnable" data-id="${dept.deptId}"
                                       value="启用">
                            </c:if>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
            <ul id="deptPageHelper"></ul>
        </div>
    </div>
</div>
</body>

</html>
